package ch04.d3;

import org.junit.Test;

public class AnimalTest {

    @Test
    public void testDog(){
        Dog dog = new Dog();
        dog.nickName = "旺财";
        dog.color = "白色";
        dog.eat();
        dog.roar();
        dog.watchDoor();
    }

    @Test
    public void testAnimal(){
        //多态：同一个事物，有多种形态
        Animal animal = new Dog();
        animal.nickName = "小花";
        animal.color = "花色";
        animal.eat();
        animal.roar();//new谁就调用谁的方法
        //编译报错，因为Animal类中没有catchMouse()方法
//        animal.catchMouse();
        // 判断animal是否是Dog的实例
        if(animal instanceof Dog){
            Dog c=(Dog)animal;//向下转型 强制类型转换
            c.watchDoor();
        }else if(animal instanceof Cat){
            Cat c=(Cat)animal;//向下转型 强制类型转换
            c.catchMouse();
        }


    }


    @Test
    public void testPerson(){
        Person p = new Person();
        Dog d=new Dog();
        Cat c=new Cat();
        p.feed(d);

    }



}















